package fun.sboy.headacheMemo.service.impl;

import fun.sboy.headacheMemo.commons.lang.CONST;
import fun.sboy.headacheMemo.dto.ArticleSearch;
import fun.sboy.headacheMemo.entry.Article;
import fun.sboy.headacheMemo.mapper.ArticleMapper;
import fun.sboy.headacheMemo.service.ArticleService;
import fun.sboy.headacheMemo.util.RequestContextHolderUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Comparator;
import java.util.List;


@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<Article> selectListArticle(ArticleSearch search) {

        //如果搜搜为当前用户的文章，则uid不为空。
        System.out.println("搜索内容：" + search);
        if (ObjectUtils.isEmpty(search.getUId())) {
            String uid = RequestContextHolderUtil.getCurrentUserId();
            search.setUId(uid);
        }

        //查询redis中是否存在文章查看次数
        List<Article> list = articleMapper.selectListArticle(search);
        list.stream().forEach(item -> {
            Object count = redisTemplate.boundHashOps(CONST.REDIS_ARTICLE_VIEW).get(item.getAId());
            if (!ObjectUtils.isEmpty(count)) {
                item.setViewCount(Long.parseLong((String) count));
            } else {
                redisTemplate.boundHashOps(CONST.REDIS_ARTICLE_VIEW).put(item.getAId().toString(), item.getViewCount().toString());
            }
        });
        //使用
        list.sort(Comparator.comparingLong(Article::getViewCount).reversed());
        return list;
    }

    @Override
    public void insertArticle(Article article) {
        String uid = RequestContextHolderUtil.getCurrentUserId();
        article.setAuthorId(uid);
        if (articleMapper.insertArticle(article) == 0) {
            throw new RuntimeException("添加文章失败！");
        }
    }

    @Override
    public List<Article> selectHotArticle() {
        return articleMapper.selectHotArticle();
    }

    @Override
    public Article selectArticleById(String aId) {
        Article article = articleMapper.selectArticleByAid(aId);
        if (article == null) {
            throw new RuntimeException("文章走丢了~");
        }

        //文章阅览次数+1
        Object o = redisTemplate.boundHashOps(CONST.REDIS_ARTICLE_VIEW).get(article.getAId());
        redisTemplate.boundHashOps(CONST.REDIS_ARTICLE_VIEW).put(article.getAId(), String.valueOf(Long.parseLong((String) o) + 1));
        return article;
    }

    @Override
    public void updateArticle(Article article) {
        String uid = RequestContextHolderUtil.getCurrentUserId();
        article.setAuthorId(uid);
        if (articleMapper.updateArticle(article) < 1) {
            throw new RuntimeException("不要妄图修改不属于自己的文章哦~");
        }
    }

    @Override
    public void deleteArticle(String aid) {
        String uid = RequestContextHolderUtil.getCurrentUserId();
        if (articleMapper.deleteArticleByAid(aid, uid) < 1) {
            throw new RuntimeException("不要妄想删除不属于自己的东西哦");
        }
    }
}
